export async function login() {
    const username = document.getElementById('loginUsername').value;
    const password = document.getElementById('loginPassword').value;

    const usernameRegex = /^[a-zA-Z0-9]{3,10}$/;
    const passwordRegex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{10,}$/;

    if (!usernameRegex.test(username)) {
        document.getElementById('loginUsername').placeholder = '用户名不符合条件（字母和数字，3 - 10 位）';
        alert('用户名不符合条件');
        return;
    }

    if (!passwordRegex.test(password)) {
        document.getElementById('loginPassword').placeholder = '密码不符合条件（包含数字、大写字母和小写字母，不少于 10 位）';
        alert('密码不符合条件');
        return;
    }

    const response = await fetch('/login', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ username, password })
    });
    const data = await response.json();
    alert(data.message);
    if (data.message === '登录成功') {
        setTimeout(() => {
            window.location.href = 'success.html?username=' + username;
        }, 500);
    }
}